<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
	<form:form commandName="jxQuestionFilter"  action="leading" method="post" enctype="multipart/form-data" cssClass="form-horizontal" role="form">
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="filename">请选择文件：</label>
			<div class="col-sm-5" id="file">
				<input class="form-control" id="filename" type="file" name="file" onchange="checkFileType(this)"/>
			</div>
		</div>
	</form:form>
<script type="text/javascript">
	function checkFileType(obj,filetype){
		var file = obj.value.match(/[^\/\\]+$/gi)[0];
		var filetype = ['xls','xlsx'];
		var msg = "导入有误";
		var checkFlag = false;
		for(var i=0;i<filetype.length;i++){
			if(i==filetype.length-1){
				msg=msg+filetype[i];
			}else{
				msg=msg+filetype[i]+",";
			}
		}
		if(file.split(".").length==2){
			file = file.split(".")[0]+"."+file.split(".")[1].toLowerCase();
		}else{	
			alert(msg);
			$("#file").html("<input  type='file' name='file' onchange='checkFileType(this)'/>");
			hideUI();
			return;
		}
		for(var i=0;i<filetype.length;i++){
			var reg = new RegExp('\\.('+(filetype[i]?filetype[i]:'')+')$','gi');
			if(file.match(reg)){
				checkFlag = true;
				break;
			}
		}
		if(!checkFlag){
			alert(msg);
			$("#file").html("<input  type='file' name='file' onchange='checkFileType(this)'/>");
			hideUI();
			return;
		}
		
		formdata = new FormData();
		formdata.append('file', obj.files[0]);
		$("#formDialog form").submit();
	}
	
	$('#formDialog form').submit(function() {
		$.ajax({
			type : 'POST',
			url :  getUrl("leading"),
			data : formdata,
			contentType : false,
			processData : false
		}).then(function(data) {
			if (data.code == "200") {
				alert(data.message);
				$('#formDialog').modal('hide');
				reload("#gridTable");
			} else {
				alert("文件上传失败，"+data.message);
			}
		}, function(data) {
			alert("文件上传失败，"+data.message);
		});
		return false;
	});
	</script>
